<?xml version="1.0" encoding="UTF-8"?>
<!--
* @author ${parametersConfig.author}
* @date ${date}
* Code Generate By RateCode
* Code Generate Gitee : https://gitee.com/GaopanL/rateCode
-->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${parametersConfig.packPath}.dao.${table.tableNameConverted}Dao">

    <resultMap id="baseMap" type="${parametersConfig.packPath}.model.${table.tableNameConverted}">
    <#list table.fieldList as field>
        <result column="${field.fieldName}" property="${field.fieldNameConverted}"/>
    </#list>
    </resultMap>

    <sql id="allCols">
    <#list table.fieldList as field>${field.fieldName} <#if (field_index < (table.fieldList?size - 1))>, </#if></#list>
    </sql>

    <sql id="queryParams">
        <where>
            deleted = '1'
        <#list table.fieldList as field>
            <if test="${field.fieldNameConverted} != null">
                and ${field.fieldName} = <#noparse>#{</#noparse>${field.fieldNameConverted}<#noparse>}</#noparse>
            </if>
        </#list>
        </where>
    </sql>

    <insert id="insert" useGeneratedKeys="true" keyColumn="${table.fieldList[0].fieldName}" keyProperty="${table.fieldList[0].fieldNameConverted}">
        insert into ${table.tableName}
        (
        <#list table.fieldList as field>
            <#if !entityConfig.excludeFileds?seq_contains(field.fieldNameConverted)>
            <if test="${field.fieldNameConverted} != null">
                ${field.fieldName} <#if (field_index < (table.fieldList?size - 1)) >,</#if>
            </if>
            </#if>
        </#list>
        )
        values(
        <#list table.fieldList as field>
            <#if !entityConfig.excludeFileds?seq_contains(field.fieldNameConverted)>
            <if test="${field.fieldNameConverted} != null">
                <#noparse>#{</#noparse>${field.fieldNameConverted}<#noparse>}</#noparse> <#if (field_index < (table.fieldList?size - 1))>,</#if>
            </if>
            </#if>
        </#list>
        )
    </insert>

    <update id="update">
        update ${table.tableName}
        <set>
            <#list table.fieldList as field>
            <if test="${field.fieldNameConverted} != null">
                ${field.fieldName} = <#noparse>#{</#noparse>${field.fieldNameConverted}<#noparse>}</#noparse> ,
            </if>
            </#list>
        </set>
        where ${table.fieldList[0].fieldName} = <#noparse>#{</#noparse>${table.fieldList[0].fieldNameConverted}<#noparse>}</#noparse>
    </update>

    <select id="queryByPage" resultMap="baseMap">
        select * from ${table.tableName}
        <include refid="queryParams"/>
        limit <#noparse>#{</#noparse>page.offset<#noparse>}</#noparse>,<#noparse>#{</#noparse>page.limit<#noparse>}</#noparse>
    </select>

    <select id="queryTotal" resultType="java.lang.Long">
        select count(*) from ${table.tableName}
        <include refid="queryParams"/>
    </select>

    <select id="queryById" resultMap="baseMap">
        select * from ${table.tableName}
        where ${table.fieldList[0].fieldName} = <#noparse>#{</#noparse>${table.fieldList[0].fieldNameConverted}<#noparse>}</#noparse>
        limit 1
    </select>

    <select id="deleteById">
        update ${table.tableName}
        set deleted = '1'
        where ${table.fieldList[0].fieldName} = <#noparse>#{</#noparse>${table.fieldList[0].fieldNameConverted}<#noparse>}</#noparse>
    </select>

</mapper>


